package com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson;

import com.lczyfz.istep.common.constants.SceneConstants;
import com.lczyfz.istep.modules.openstack.api.neutron.model.*;
import com.lczyfz.istep.modules.openstack.api.neutron.model.firewall.FireWall;
import com.lczyfz.istep.modules.openstack.api.neutron.model.firewall.FireWallPolicy;
import com.lczyfz.istep.modules.openstack.api.neutron.model.firewall.FireWallRule;
import com.lczyfz.istep.modules.openstack.api.neutron.model.firewall.FireWallRuleInsert;
import com.lczyfz.istep.modules.openstack.api.neutron.model.securitygroup.Securitygroup;
import com.lczyfz.istep.modules.openstack.api.neutron.model.securitygroup.SecuritygroupRule;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;

/**
 * 用于封装和解析Neutron API 的相关参数JSON
 */
public class NeutronParse {

	/**
	 * 解析取所有路由返回的结果的JSON
	 * 用于RouterImpl类的getRouters方法
	 * @param routersJson
	 * @return
	 */
	public static List<Router> resolveRouters(JSONObject routersJson){

		List<Router> routerList = new ArrayList<Router>();
		JSONArray routers = routersJson.getJSONArray("routers");
		for(int i = 0 ; i < routers.size(); i++){
			Router router = new Router();
			router.setRouterName(routers.getJSONObject(i).getString("name"));
			router.setRouterId(routers.getJSONObject(i).getString("id"));
			router.setRouterStatus(routers.getJSONObject(i).getString("status"));
			router.setRouterProject(routers.getJSONObject(i).getString("project_id"));
			router.setRouterTenant(routers.getJSONObject(i).getString("tenant_id"));
			routerList.add(router);
		}

		return routerList;

	}
	/**
	 * 解析取指定路由详情返回的结果的JSON
	 * 用于RouterImpl类的getRouterDetail方法
	 * @param routerJson
	 * @return
	 */
	public static Router getRouterDetail(JSONObject routerJson){

		JSONObject routers = routerJson.getJSONObject("router");
		Router router = new Router();
		router.setRouterName(routers.getString("name"));
		router.setRouterId(routers.getString("id"));
		router.setRouterStatus(routers.getString("status"));
		router.setRouterProject(routers.getString("project_id"));
		router.setRouterTenant(routers.getString("tenant_id"));
//			router.setRouterNetworkId(routers.getJSONObject("external_gateway_info").getString("network_id"));
		Map<String,String> subnetIds = new HashMap<String,String>();
		for(int i = 0 ; i < routers.getJSONArray("routes").size() ; i++){
			subnetIds.put(routers.getJSONArray("routes")
					.getJSONObject(i).getString("destination"),routers.getJSONArray("routes")
					.getJSONObject(i).getString("nexthop"));
		}
		router.setRouters(subnetIds);

		return router;

	}
	/**
	 *  封装创建路由所需要传的参数
	 * 用于RouterImpl类的createRouter方法
	 * @param routerinput
	 * @return
	 */
	public static JSONObject packageRouter(Router routerinput){

		JSONObject params = new JSONObject();
		JSONObject router = new JSONObject();
		JSONObject external_gateway_info = new JSONObject();
//		JSONArray  external_fixed_ips = new JSONArray();
//		JSONObject subnet_id = new JSONObject();
//		JSONObject ip_address = new JSONObject();
//		JSONObject partparams = new JSONObject();
		if(routerinput.getRouterName()!=null)router.put("name", routerinput.getRouterName());
		if(routerinput.getRouterId()!=null)router.put("id", routerinput.getRouterId());
		if(routerinput.getRouterProject()!=null)router.put("project_id", routerinput.getRouterProject());
		if(routerinput.getRouterTenant()!=null)router.put("tenant_id", routerinput.getRouterTenant());
		if(routerinput.getRouterStatus()!=null)router.put("status", routerinput.getRouterStatus());
		if(routerinput.getRouterNetworkId()!=null){
			external_gateway_info.put("network_id", routerinput.getRouterNetworkId());
		}
//		if(routerinput.getRouters()!=null){
//			Iterator i = routerinput.getRouters().entrySet().iterator();
//			while(i.hasNext()) {
//				Entry entry = (java.util.Map.Entry)i.next();
//				partparams.put("destination", entry.getKey());
//				partparams.put("nexthop", entry.getValue());
//				external_fixed_ips.add(partparams);
//
//			}
////			for(int i = 0 ; i < routerinput.getExternalIps().size() ; i++){
////			//	subnet_id.put("subnet_id", routerinput.getExternalIps().get(i));
////				partparams.put("ip_address", routerinput.getExternalIps().get(i));
////				partparams.put("subnet_id", routerinput.getExternalIps().get(i));
////				external_fixed_ips.add(partparams);
////			}
//			router.put("routes" , external_fixed_ips);
//		}
		router.put("external_gateway_info", external_gateway_info);
		params.put("router",router);

		return params;

	}
	/**
	 * 解析取创建路由返回的结果的JSON
	 * 用于RouterImpl类的createRouter方法
	 * @param routerJson
	 * @return
	 */
	public static Router resolveCreateRouter(JSONObject routerJson){

		JSONObject routers = routerJson.getJSONObject("router");
		Router router = new Router();
		router.setRouterName(routers.getString("name"));
		router.setRouterId(routers.getString("id"));
		router.setRouterStatus(routers.getString("status"));
		router.setRouterProject(routers.getString("project_id"));
		router.setRouterTenant(routers.getString("tenant_id"));

//		router.setRouterNetworkId(routers.getJSONObject("external_gateway_info").getString("network_id"));
		Map<String,String> subnetIds = new HashMap<String,String>();
		for(int i = 0 ; i < routers.getJSONArray("routes").size() ; i++){
			subnetIds.put(routers.getJSONArray("routes")
					.getJSONObject(i).getString("destination"),routers.getJSONArray("routes")
					.getJSONObject(i).getString("nexthop"));
		}
		router.setRouters(subnetIds);
		return router;

	}
	/**
	 * 封装添加接口到路由器所需要传的参数
	 * 用于RouterImpl类的addRouterInterface方法
	 * @param id 子网id或端口id
	 * @return
	 */
	public static JSONObject packageRouterInterface(String id,int type){

		JSONObject params = new JSONObject();
		if(type== SceneConstants.routerAndRouter)
			params.put("port_id",id);
		else if(type==SceneConstants.routerAndNetwork)
			params.put("subnet_id",id);
		return params;

	}
	/**
	 * 解析取添加接口到路由器返回的结果的JSON
	 * 用于RouterImpl类的addRouterInterface方法
	 * @param routerJson
	 * @return
	 */
	public static Router resolveAddRouterInterface(JSONObject routerJson){

		Router router = new Router();
		router.setRouterId(routerJson.getString("id"));
		router.setRouterNetworkId(routerJson.getString("network_id"));
		ArrayList<String> subnetIds = new ArrayList<String>();
		for(int i = 0 ; i < routerJson.getJSONArray("subnet_ids").size() ; i++){
			subnetIds.add(routerJson.getJSONArray("subnet_ids").getString(i));
		}
		router.setRouterSubnetId(subnetIds);
		//	router.setRouterProject(routerJson.getString("project_id"));
		router.setRouterTenant(routerJson.getString("tenant_id"));

		return router;

	}
	/**
	 * 解析取从路由器删除接口返回的结果的JSON
	 * 用于RouterImpl类的removeRouterInterface方法
	 * @param routerJson
	 * @return
	 */
	public static Router resolveRemoveRouterInterface(JSONObject routerJson){

		Router router = new Router();
		router.setRouterId(routerJson.getString("id"));
		router.setRouterNetworkId(routerJson.getString("network_id"));
		ArrayList<String> subnetIds = new ArrayList<String>();
		for(int i = 0 ; i < routerJson.getJSONArray("subnet_ids").size() ; i++){
			subnetIds.add(routerJson.getJSONArray("subnet_ids").getString(i));
		}
		router.setRouterSubnetId(subnetIds);
		//	router.setRouterProject(routerJson.getString("project_id"));
		router.setRouterTenant(routerJson.getString("tenant_id"));

		return router;

	}
	/**
	 * 封装更新路由所需要传的参数
	 * 用于RouterImpl类的updateRouter方法
	 * @param routerupdateinput
	 * @return
	 */
	public static JSONObject packageUpdateRouter( Router routerupdateinput){

//		JSONObject params = new JSONObject();
//		JSONObject router = new JSONObject();
//		JSONObject external_gateway_info = new JSONObject();
//		JSONArray  external_fixed_ips = new JSONArray();
//		//	JSONObject subnet_id = new JSONObject();
//		JSONObject partparams = new JSONObject();
//		if(routerupdateinput.getRouterName()!=null)router.put("name", routerupdateinput.getRouterName());
//		if(routerupdateinput.getRouterId()!=null)router.put("id", routerupdateinput.getRouterId());
//		if(routerupdateinput.getRouterProject()!=null)router.put("project_id",routerupdateinput.getRouterProject());
//		if(routerupdateinput.getRouterTenant()!=null)router.put("tenant_id", routerupdateinput.getRouterTenant());
//		if(routerupdateinput.getRouterStatus()!=null)router.put("status", routerupdateinput.getRouterStatus());
//
//		if(routerupdateinput.getRouterNetworkId()!=null){
//			external_gateway_info.put("network_id", routerupdateinput.getRouterNetworkId());
//		}
//		if(routerupdateinput.getRouters()!=null){
//			@SuppressWarnings("rawtypes")
//			Iterator i = routerupdateinput.getRouters().entrySet().iterator();
//			while(i.hasNext()) {
//				@SuppressWarnings("rawtypes")
//				Map.Entry entry = (java.util.Map.Entry)i.next();
//				partparams.put("destination", entry.getKey());
//				partparams.put("nexthop", entry.getValue());
//				external_fixed_ips.add(partparams);
//
//			}
////			for(int i = 0 ; i < routerinput.getExternalIps().size() ; i++){
////			//	subnet_id.put("subnet_id", routerinput.getExternalIps().get(i));
////				partparams.put("ip_address", routerinput.getExternalIps().get(i));
////				partparams.put("subnet_id", routerinput.getExternalIps().get(i));
////				external_fixed_ips.add(partparams);
////			}
//			//	external_gateway_info.put("routes" , external_fixed_ips);
//		}
//		router.put("routes", external_fixed_ips);
//		params.put("router",router);
//
//		return params;
		JSONObject params = new JSONObject();
		JSONObject router = new JSONObject();
		JSONObject external_gateway_info = new JSONObject();
		JSONArray  external_fixed_ips = new JSONArray();
		//	JSONObject subnet_id = new JSONObject();
		JSONObject partparams = new JSONObject();
		if(routerupdateinput.getRouterName()!=null)router.put("name", routerupdateinput.getRouterName());
		if(routerupdateinput.getRouterId() != null)router.put("id", routerupdateinput.getRouterId());
		if(routerupdateinput.getRouterProject()!=null)router.put("project_id",routerupdateinput.getRouterProject());
		if(routerupdateinput.getRouterTenant()!=null)router.put("tenant_id", routerupdateinput.getRouterTenant());
		if(routerupdateinput.getRouterStatus()!=null)router.put("status", routerupdateinput.getRouterStatus());

		if(routerupdateinput.getRouterNetworkId()!=null){
			external_gateway_info.put("network_id", routerupdateinput.getRouterNetworkId());
		}
		if(routerupdateinput.getRouters()!=null){
			Iterator i = routerupdateinput.getRouters().entrySet().iterator();
			while(i.hasNext()) {
				Map.Entry entry = (java.util.Map.Entry)i.next();
				partparams.put("destination", entry.getKey());
				partparams.put("nexthop", entry.getValue());
				external_fixed_ips.add(partparams);

			}
//			for(int i = 0 ; i < routerinput.getExternalIps().size() ; i++){
//			//	subnet_id.put("subnet_id", routerinput.getExternalIps().get(i));
//				partparams.put("ip_address", routerinput.getExternalIps().get(i));
//				partparams.put("subnet_id", routerinput.getExternalIps().get(i));
//				external_fixed_ips.add(partparams);
//			}
			//	external_gateway_info.put("routes" , external_fixed_ips);
		}
		router.put("routes", external_fixed_ips);
		params.put("router",router);

		return params;

	}
	/**
	 * 解析取更新路由返回的结果的JSON
	 * 用于RouterImpl类的updateRouter方法
	 * @param routerJson
	 * @return
	 */
	public static Router resolveUpdateRouter(JSONObject routerJson){

		Router router = new Router();
		JSONObject routers = routerJson.getJSONObject("router");
		router.setRouterId(routers.getString("id"));
		router.setRouterName(routers.getString("name"));
		router.setRouterStatus(routers.getString("status"));
		router.setRouterTenant(routers.getString("tenant_id"));

//		router.setRouterNetworkId(routers.getJSONObject("external_gateway_info").getString("network_id"));
		Map<String,String> subnetIds = new HashMap<String,String>();
		for(int i = 0 ; i < routers.getJSONArray("routes").size() ; i++){
			subnetIds.put(routers.getJSONArray("routes")
					.getJSONObject(i).getString("destination"),routers.getJSONArray("routes")
					.getJSONObject(i).getString("nexthop"));
		}
		router.setRouters(subnetIds);
		return router;

	}

	/**
	 * 解析所有网络返回的结果的JSON
	 * 用于NetworksImpl类的getNetwork方法
	 * @param networksJson
	 * @return
	 */
	public static List<Network> resolveNetworks(JSONObject networksJson) {

		List<Network> networkList = new ArrayList<Network>();
		JSONArray networks = networksJson.getJSONArray("networks");
		// System.out.println(networks);
		for (int i = 0; i < networks.size(); i++) {
			Network network = new Network();
			network.setId(networks.getJSONObject(i).getString("id"));
			network.setName(networks.getJSONObject(i).getString("name"));
			// System.out.println(network.getId() + " " + network.getName());
			networkList.add(network);
		}

		return networkList;

	}

	/**
	 * 解析取所有网络详情返回的结果的JSON
	 * 用于NetworkImpl类的getNetworkDetail方法
	 * @param networksJson
	 * @return
	 */
	public static Network resolveNetworksDetail(JSONObject networksJson) {
		JSONObject networks = networksJson.getJSONObject("network");
		Network network = new Network();
		network.setId(networks.getString("id"));
		network.setName(networks.getString("name"));
		network.setStatus(networks.getString("status"));
		JSONArray subnets = networks.getJSONArray("subnets");
		int size = subnets.size();
		List<String> subnetIds = new ArrayList<String>();
		for(int i = 0; i < size ; i++){
			subnetIds.add(subnets.getString(i));
		}
		network.setSubnets(subnetIds);
		return network;
	}

	/**
	 * 封装创建网络所需要传的参数
	 * 用于NetworksImpl类的createNetwork方法
	 * @param network
	 * @return
	 */
	public static JSONObject packageNetwork(Network network) {
		JSONObject params = new JSONObject();
		JSONObject networks = new JSONObject();
		if(network.getName()!=null)networks.put("name", network.getName());
		params.put("network", networks);
		return params;
	}

	/**
	 * 解析创建网络返回的JSon
	 * @param networksJson
	 * @return
	 */
	public static Network resolveCreateNetwork(JSONObject networksJson) {
		JSONObject networks = networksJson.getJSONObject("network");
		Network network = new Network();
		network.setId(networks.getString("id"));
		network.setName(networks.getString("name"));
		network.setStatus(networks.getString("status"));
		//	network.setSubnets(networks.getString("subnet"));
		return network;
	}

	/**
	 * 解析更新网络返回的JSon
	 * @param networksJson
	 * @return
	 */
	public static Network resolveUpdateNetwork(JSONObject networksJson) {
		JSONObject networks = networksJson.getJSONObject("network");
		Network network = new Network();
		network.setId(networks.getString("id"));
		network.setName(networks.getString("name"));
		network.setStatus(networks.getString("status"));
		//	network.setSubnets(networks.getString("subnet"));
		return network;
	}

	/**
	 * 解析取所有子网池返回的结果的JSON
	 * 用于SubnetPoolImpl类的getSubnetPools方法
	 * @param subnetpoolsJson
	 * @return
	 */
	public static List<SubnetPool> resolveSubnetPools(JSONObject subnetpoolsJson){

		List<SubnetPool> splist = new ArrayList<SubnetPool>();
		JSONArray subnetpools = subnetpoolsJson.getJSONArray("subnetpools");
		for(int i = 0 ;i < subnetpools.size() ; i++){
			SubnetPool sp = new SubnetPool();
			sp.setSpId(subnetpools.getJSONObject(i).getString("id"));
			sp.setSpName(subnetpools.getJSONObject(i).getString("name"));
			sp.setSpTenantId(subnetpools.getJSONObject(i).getString("tenant_id"));
			sp.setSpProjectId(subnetpools.getJSONObject(i).getString("project_id"));
			sp.setSpIsDefault(subnetpools.getJSONObject(i).getString("is_default"));
			sp.setSpIpVersion(subnetpools.getJSONObject(i).getString("ip_version"));
			sp.setSpDefaultPrefixlen(subnetpools.getJSONObject(i).getString("default_prefixlen"));
			ArrayList<String> spPrefixesList = new ArrayList<String>();
			JSONArray spPrefixes = subnetpools.getJSONObject(i).getJSONArray("prefixes");
			for(int j = 0 ; j < spPrefixes.size() ; j++){
				spPrefixesList.add(spPrefixes.getString(j));
			}
			sp.setSpPrefixes(spPrefixesList);
			splist.add(sp);
		}

		return splist;

	}

	/**
	 * 解析取指定子网池详情返回的结果的JSON
	 * 用于SubnetPoolImpl类的getSubnetPoolDetail方法
	 * @param subnetpoolsJson
	 * @return
	 */
	public static SubnetPool resolveSubnetPoolDetail(JSONObject subnetpoolsJson){

		JSONObject subnetpool = subnetpoolsJson.getJSONObject("subnetpool");
		SubnetPool sp = new SubnetPool();
		sp.setSpId(subnetpool.getString("id"));
		sp.setSpName(subnetpool.getString("name"));
		sp.setSpTenantId(subnetpool.getString("tenant_id"));
		sp.setSpProjectId(subnetpool.getString("project_id"));
		sp.setSpIsDefault(subnetpool.getString("is_default"));
		sp.setSpCreatedAt(subnetpool.getString("created_at"));
		sp.setSpUpdatedAt(subnetpool.getString("updated_at"));
		sp.setSpIpVersion(subnetpool.getString("ip_version"));
		sp.setSpDefaultPrefixlen(subnetpool.getString("default_prefixlen"));
		ArrayList<String> spPrefixesList = new ArrayList<String>();
		JSONArray spPrefixes = subnetpool.getJSONArray("prefixes");
		for(int j = 0 ; j < spPrefixes.size() ; j++){
			spPrefixesList.add(spPrefixes.getString(j));
		}
		sp.setSpPrefixes(spPrefixesList);
		//	router.setRouterNetworkId(routers.getJSONObject("external_gateway_info").getString("network_id"));

		return sp;

	}

	/**
	 *  封装创建或更新子网池所需要传的参数
	 * 用于SubnetPoolImpl类的createSubnetPool或updateSubnetPool方法
	 * @param spinput
	 * @return
	 */
	public static JSONObject packageSubnetPool(SubnetPool spinput){

		JSONObject params = new JSONObject();
		JSONObject subnetpool = new JSONObject();
		JSONArray prefixe = new JSONArray();
		if(spinput.getSpId()!=null)subnetpool.put("id", spinput.getSpId());
		if(spinput.getSpName()!=null)subnetpool.put("name", spinput.getSpName());
		if(spinput.getSpProjectId()!=null)subnetpool.put("project_id", spinput.getSpProjectId());
		if(spinput.getSpTenantId()!=null)subnetpool.put("tenant_id", spinput.getSpTenantId());
		if(spinput.getSpCreatedAt()!=null)subnetpool.put("created_at", spinput.getSpCreatedAt());
		if(spinput.getSpUpdatedAt()!=null)subnetpool.put("updated_at", spinput.getSpUpdatedAt());
		if(spinput.getSpDefaultPrefixlen()!=null)subnetpool.put("default_prefixlen", spinput.getSpDefaultPrefixlen());
		if(spinput.getSpIpVersion()!=null)subnetpool.put("ip_version", spinput.getSpIpVersion());
		if(spinput.getSpIsDefault()!=null)subnetpool.put("is_default", spinput.getSpIsDefault());
		if(spinput.getSpPrefixes()!=null){
			for(int i = 0 ; i < spinput.getSpPrefixes().size() ; i++){
				prefixe.add(spinput.getSpPrefixes().get(i));
			}
			subnetpool.put("prefixes" , prefixe);
		}
		params.put("subnetpool",subnetpool);

		return params;

	}

	/**
	 * 解析取创建子网池返回的结果的JSON
	 * 用于SubnetPoolImpl类的createSubnetPool方法
	 * @param subnetpoolJson
	 * @return
	 */
	public static SubnetPool resolveCreateSubnetPool(JSONObject subnetpoolJson){

		JSONObject subnetpool = subnetpoolJson.getJSONObject("subnetpool");
		SubnetPool sp = new SubnetPool();
		sp.setSpId(subnetpool.getString("id"));
		sp.setSpName(subnetpool.getString("name"));
		sp.setSpTenantId(subnetpool.getString("tenant_id"));
		sp.setSpProjectId(subnetpool.getString("project_id"));
		sp.setSpIsDefault(subnetpool.getString("is_default"));
		sp.setSpIpVersion(subnetpool.getString("ip_version"));
		sp.setSpDefaultPrefixlen(subnetpool.getString("default_prefixlen"));
		ArrayList<String> spPrefixesList = new ArrayList<String>();
		JSONArray spPrefixes = subnetpool.getJSONArray("prefixes");
		for(int j = 0 ; j < spPrefixes.size() ; j++){
			spPrefixesList.add(spPrefixes.getString(j));
		}
		sp.setSpPrefixes(spPrefixesList);

		return sp;

	}

	/**
	 * 解析取更新子网池返回的结果的JSON
	 * 用于SubnetPoolImpl类的updateSubnetPool方法
	 * @param subnetpoolJson
	 * @return
	 */
	public static SubnetPool resolveUpdateSubnetPool(JSONObject subnetpoolJson){

		JSONObject subnetpool = subnetpoolJson.getJSONObject("subnetpool");
		SubnetPool sp = new SubnetPool();
		sp.setSpId(subnetpool.getString("id"));
		sp.setSpName(subnetpool.getString("name"));
		sp.setSpTenantId(subnetpool.getString("tenant_id"));
		//sp.setSpProjectId(subnetpool.getString("project_id"));
		sp.setSpIsDefault(subnetpool.getString("is_default"));
		sp.setSpIpVersion(subnetpool.getString("ip_version"));
		sp.setSpDefaultPrefixlen(subnetpool.getString("default_prefixlen"));
		ArrayList<String> spPrefixesList = new ArrayList<String>();
		JSONArray spPrefixes = subnetpool.getJSONArray("prefixes");
		for(int j = 0 ; j < spPrefixes.size() ; j++){
			spPrefixesList.add(spPrefixes.getString(j));
		}
		sp.setSpPrefixes(spPrefixesList);

		return sp;

	}

	/**
	 * 解析取所有子网返回的结果的JSON
	 * 用于SubnetImpl类的getSubnets方法
	 * @param subnetsJson
	 * @return
	 */
	public static List<Subnet> resolveSubnets(JSONObject subnetsJson){

		List<Subnet> subnetlist = new ArrayList<Subnet>();
		JSONArray subnets = subnetsJson.getJSONArray("subnets");
		for(int i = 0 ;i < subnets.size() ; i++){
			Subnet subnet = new Subnet();
			subnet.setsubnetId(subnets.getJSONObject(i).getString("id"));
			subnet.setsubnetName(subnets.getJSONObject(i).getString("name"));
			subnet.setsubnetNetworkId(subnets.getJSONObject(i).getString("network_id"));
			subnet.setsubnetProjectId(subnets.getJSONObject(i).getString("project_id"));
			subnet.setsubnetTenantId(subnets.getJSONObject(i).getString("tenant_id"));
			subnet.setsubnetCidr(subnets.getJSONObject(i).getString("cidr"));
			subnet.setsubnetIpVersion(subnets.getJSONObject(i).getString("ip_version"));
			subnet.setsubnetGatewayIp(subnets.getJSONObject(i).getString("gateway_ip"));
			subnet.setsubnetUpdateAt(subnets.getJSONObject(i).getString("updated_at"));
			subnet.setsubnetCreateAt(subnets.getJSONObject(i).getString("created_at"));
			subnetlist.add(subnet);
		}

		return subnetlist;

	}

	/**
	 * 解析取指定子网详情或创建子网返回的结果的JSON
	 * 用于SubnetImpl类的getSubnetDetail或createSubnet方法
	 * @param subnetJson
	 * @return
	 */
	public static Subnet resolveSubnetDetail(JSONObject subnetJson){

		JSONObject subnets = subnetJson.getJSONObject("subnet");
		Subnet subnet = new Subnet();
		subnet.setsubnetId(subnets.getString("id"));
		subnet.setsubnetName(subnets.getString("name"));
		subnet.setsubnetNetworkId(subnets.getString("network_id"));
		subnet.setsubnetProjectId(subnets.getString("project_id"));
		subnet.setsubnetTenantId(subnets.getString("tenant_id"));
		subnet.setsubnetCidr(subnets.getString("cidr"));
		subnet.setsubnetIpVersion(subnets.getString("ip_version"));
		subnet.setsubnetGatewayIp(subnets.getString("gateway_ip"));
		subnet.setsubnetUpdateAt(subnets.getString("updated_at"));
		subnet.setsubnetCreateAt(subnets.getString("created_at"));

		return subnet;

	}

	/**
	 * 封装创建子网或更新子网所需要传的参数
	 * 用于SubnetImpl类的createSubnet或updateSubnet方法
	 * @param subnetinput
	 * @return
	 */
	public static JSONObject packageSubnet(Subnet subnetinput){

		JSONObject params = new JSONObject();
		JSONObject subnet = new JSONObject();
		JSONArray dnsnameservers = new JSONArray();
		JSONArray hostroutes = new JSONArray();
		if(subnetinput.getsubnetId()!=null)subnet.put("id",subnetinput.getsubnetId());
		if(subnetinput.getsubnetName()!=null)subnet.put("name", subnetinput.getsubnetName());
		if(subnetinput.getsubnetNetworkId()!=null)subnet.put("network_id",subnetinput.getsubnetNetworkId());
		if(subnetinput.getsubnetCidr()!=null)subnet.put("cidr",subnetinput.getsubnetCidr());
		if(subnetinput.getsubnetProjectId()!=null)subnet.put("project_id",subnetinput.getsubnetProjectId());
		if(subnetinput.getsubnetTenantId()!=null)subnet.put("tenant_id",subnetinput.getsubnetTenantId());
		if(subnetinput.getsubnetCreateAt()!=null)subnet.put("created_at",subnetinput.getsubnetCreateAt());
		if(subnetinput.getsubnetUpdateAt()!=null)subnet.put("updated_at",subnetinput.getsubnetUpdateAt());
		if(subnetinput.getsubnetPoolId()!=null)subnet.put("subnetpool_id", subnetinput.getsubnetPoolId());

		if(subnetinput.getsubnetIpVersion()!=null)subnet.put("ip_version" , subnetinput.getsubnetIpVersion());
		if(subnetinput.getsubnetDnsNameServers()!=null){
			for(int i = 0 ; i < subnetinput.getsubnetDnsNameServers().size() ; i++){
				dnsnameservers.add(subnetinput.getsubnetDnsNameServers().get(i));
			}
			subnet.put("dns_nameservers" , dnsnameservers);
		}
		if(subnetinput.getsubnetHostRoutes()!=null){
			for(int i = 0 ; i < subnetinput.getsubnetHostRoutes().size() ; i++){
				hostroutes.add(subnetinput.getsubnetHostRoutes().get(i));
			}
			subnet.put("host_routes" , hostroutes);
		}

		String preIp = getNetworkNumber(subnetinput.getsubnetCidr());
		if(subnetinput.getsubnetGatewayIp()!=null) {
			subnet.put("gateway_ip",subnetinput.getsubnetGatewayIp());
		}else {
			String gatewayIp = preIp + ".251";
			subnet.put("gateway_ip",gatewayIp);
		}

		JSONArray allocationPools = new JSONArray();
		JSONObject firstPools = new JSONObject();
		firstPools.put("start", preIp + ".252");
		firstPools.put("end", preIp + ".254");
		allocationPools.add(firstPools);
		subnet.put("allocation_pools", allocationPools);
		params.put("subnet",subnet);

		return params;

	}

	/**
	 * 解析取更新子网返回的结果的JSON
	 * 用于SubnetImpl类的updateSubnet方法
	 * @param subnetJson
	 * @return
	 */
	public static Subnet resolveUpdateSubnet(JSONObject subnetJson){

		JSONObject subnets = subnetJson.getJSONObject("subnet");
		Subnet subnet = new Subnet();
		subnet.setsubnetId(subnets.getString("id"));
		subnet.setsubnetName(subnets.getString("name"));
		subnet.setsubnetNetworkId(subnets.getString("network_id"));
		subnet.setsubnetProjectId(subnets.getString("project_id"));
		subnet.setsubnetTenantId(subnets.getString("tenant_id"));
		subnet.setsubnetCidr(subnets.getString("cidr"));
		subnet.setsubnetIpVersion(subnets.getString("ip_version"));
		subnet.setsubnetGatewayIp(subnets.getString("gateway_ip"));

		return subnet;

	}

	/**
	 * 解析取所有安全组规则返回的结果的JSON
	 * 用于SecuritygroupRuleImpl类的 getSecuritygroupRules方法
	 * @param securitygrouprulesJson
	 * @return
	 */
	public static List<SecuritygroupRule> resolveSecuritygroupRules(JSONObject securitygrouprulesJson){

		List<SecuritygroupRule> securitygrouprulelist = new ArrayList<SecuritygroupRule>();
		JSONArray securitygrouprules = securitygrouprulesJson.getJSONArray("security_group_rules");
		for(int i = 0 ;i < securitygrouprules.size() ; i++){
			SecuritygroupRule securitygrouprule = new SecuritygroupRule();
			securitygrouprule.setSrId(securitygrouprules.getJSONObject(i).getString("id"));
			securitygrouprule.setSrDirection(securitygrouprules.getJSONObject(i).getString("direction"));
			securitygrouprule.setSrEthertype(securitygrouprules.getJSONObject(i).getString("ethertype"));
			securitygrouprule.setSrProtocol(securitygrouprules.getJSONObject(i).getString("protocol"));
			securitygrouprule.setSrTenantId(securitygrouprules.getJSONObject(i).getString("tenant_id"));
			securitygrouprule.setSrProjectId(securitygrouprules.getJSONObject(i).getString("project_id"));
			securitygrouprule.setSrRemoteGroupId(securitygrouprules.getJSONObject(i).getString("remote_group_id"));
			securitygrouprule.setSrRemoteIpPrefix(securitygrouprules.getJSONObject(i).getString("remote_ip_prefix"));
			securitygrouprule.setSrSecurityGroupId(securitygrouprules.getJSONObject(i).getString("security_group_id"));
			securitygrouprule.setSrPortMin(securitygrouprules.getJSONObject(i).getString("port_range_min"));
			securitygrouprule.setSrPortMax(securitygrouprules.getJSONObject(i).getString("port_range_max"));
			securitygrouprulelist.add(securitygrouprule);
		}

		return securitygrouprulelist;

	}

	/**
	 * 解析取指定安全组规则详情或创建安全组规则返回的结果的JSON
	 * 用于SecuritygroupRuleImpl类的getSecuritygroupRuleDetail或createSecuritygroupRule方法
	 * @param securitygrouprulesJson
	 * @return
	 */
	public static SecuritygroupRule resolveSecuritygroupRuleDetail(JSONObject securitygrouprulesJson){

		JSONObject securitygrouprules = securitygrouprulesJson.getJSONObject("security_group_rule");
		SecuritygroupRule sr = new SecuritygroupRule();
		sr.setSrId(securitygrouprules.getString("id"));
		sr.setSrDirection(securitygrouprules.getString("direction"));
		sr.setSrEthertype(securitygrouprules.getString("ethertype"));
		sr.setSrProtocol(securitygrouprules.getString("protocol"));
		sr.setSrTenantId(securitygrouprules.getString("tenant_id"));
		sr.setSrProjectId(securitygrouprules.getString("project_id"));
		sr.setSrRemoteGroupId(securitygrouprules.getString("remote_group_id"));
		sr.setSrRemoteIpPrefix(securitygrouprules.getString("remote_ip_prefix"));
		sr.setSrSecurityGroupId(securitygrouprules.getString("security_group_id"));
		sr.setSrPortMin(securitygrouprules.getString("port_range_min"));
		sr.setSrPortMax(securitygrouprules.getString("port_range_max"));
		return sr;

	}

	/**
	 * 封装创建安全组规则所需要传的参数
	 * 用于SecuritygroupRuleImpl类的createSecuritygroupRule方法
	 * @param srinput
	 * @return
	 */
	public static JSONObject packageSecuritygroupRule(SecuritygroupRule srinput){

		JSONObject params = new JSONObject();
		JSONObject securitygrouprule = new JSONObject();
		if(srinput.getSrId()!=null)securitygrouprule.put("id", srinput.getSrId());
		if(srinput.getSrDirection()!=null)securitygrouprule.put("direction",srinput.getSrDirection());
		if(srinput.getSrEthertype()!=null)securitygrouprule.put("ethertype", srinput.getSrEthertype());
		if(srinput.getSrProtocol()!=null)securitygrouprule.put("protocol", srinput.getSrProtocol());
		if(srinput.getSrRemoteGroupId()!=null)securitygrouprule.put("remote_group_id", srinput.getSrRemoteGroupId());
		if(srinput.getSrRemoteIpPrefix()!=null)securitygrouprule.put("remote_ip_prefix", srinput.getSrRemoteIpPrefix());
		if(srinput.getSrProjectId()!=null)securitygrouprule.put("project_id",srinput.getSrProjectId());
		if(srinput.getSrTenantId()!=null)securitygrouprule.put("tenant_id",srinput.getSrTenantId());
		if(srinput.getSrSecurityGroupId()!=null)securitygrouprule.put("security_group_id", srinput.getSrSecurityGroupId());
		if(srinput.getSrPortMax()!=null)securitygrouprule.put("port_range_max", srinput.getSrPortMax());
		if(srinput.getSrPortMin()!=null)securitygrouprule.put("port_range_min", srinput.getSrPortMin());
		params.put("security_group_rule",securitygrouprule);

		return params;

	}

	/**
	 * 解析取所有安全组返回的结果的JSON
	 * 用于SecuritygroupImpl类的 getSecuritygroups方法
	 * @param securitygroupsJson
	 * @return
	 */
	public static List<Securitygroup> resolveSecuritygroups(JSONObject securitygroupsJson){

		List<Securitygroup> securitygrouplist = new ArrayList<Securitygroup>();
		JSONArray securitygroups = securitygroupsJson.getJSONArray("security_groups");
		for(int i = 0 ;i < securitygroups.size() ; i++){
			Securitygroup securitygroup = new Securitygroup();
			securitygroup.setSecuritygroupId(securitygroups.getJSONObject(i).getString("id"));
			securitygroup.setSecuritygroupName(securitygroups.getJSONObject(i).getString("name"));
			securitygroup.setSecuritygroupTenantId(securitygroups.getJSONObject(i).getString("tenant_id"));
			securitygroup.setSecuritygroupProjectId(securitygroups.getJSONObject(i).getString("project_id"));

			ArrayList<SecuritygroupRule> securitygroupSr = new ArrayList<SecuritygroupRule>();
			JSONArray Sr = securitygroups.getJSONObject(i).getJSONArray("security_group_rules");
			for(int j = 0 ; j < Sr.size() ; j++){
				SecuritygroupRule sr = new SecuritygroupRule();
				sr.setSrId(Sr.getJSONObject(j).getString("id"));
				sr.setSrDirection(Sr.getJSONObject(j).getString("direction"));
				sr.setSrEthertype(Sr.getJSONObject(j).getString("ethertype"));
				sr.setSrProtocol(Sr.getJSONObject(j).getString("protocol"));
				sr.setSrRemoteGroupId(Sr.getJSONObject(j).getString("remote_group_id"));
				sr.setSrRemoteIpPrefix(Sr.getJSONObject(j).getString("remote_ip_prefix"));
				sr.setSrPortMax(Sr.getJSONObject(j).getString("port_range_max"));
				sr.setSrPortMin(Sr.getJSONObject(j).getString("port_range_min"));
				sr.setSrProjectId(Sr.getJSONObject(j).getString("project_id"));
				sr.setSrTenantId(Sr.getJSONObject(j).getString("tenant_id"));
				sr.setSrSecurityGroupId(Sr.getJSONObject(j).getString("security_group_id"));
				securitygroupSr.add(sr);
			}
			securitygroup.setsecuritygroupSr(securitygroupSr);
			securitygrouplist.add(securitygroup);
		}
		return securitygrouplist;

	}

	/**
	 * 解析取指定安全组详情或创建安全组返回的结果的JSON
	 * 用于SecuritygroupImpll类的getSecuritygroupDetail或createSecuritygroup方法
	 * @param securitygroupsJson
	 * @return
	 */
	public static Securitygroup resolveSecuritygroupDetail(JSONObject securitygroupsJson){

		JSONObject securitygroups = securitygroupsJson.getJSONObject("security_group");
		Securitygroup securitygroup = new Securitygroup();
		securitygroup.setSecuritygroupId(securitygroups.getString("id"));
		securitygroup.setSecuritygroupName(securitygroups.getString("name"));
		securitygroup.setSecuritygroupTenantId(securitygroups.getString("tenant_id"));
		securitygroup.setSecuritygroupProjectId(securitygroups.getString("project_id"));

		ArrayList<SecuritygroupRule> securitygroupSr = new ArrayList<SecuritygroupRule>();
		JSONArray Sr = securitygroups.getJSONArray("security_group_rules");
		for(int j = 0 ; j < Sr.size() ; j++){
			SecuritygroupRule sr = new SecuritygroupRule();
			sr.setSrId(Sr.getJSONObject(j).getString("id"));
			sr.setSrDirection(Sr.getJSONObject(j).getString("direction"));
			sr.setSrEthertype(Sr.getJSONObject(j).getString("ethertype"));
			sr.setSrProtocol(Sr.getJSONObject(j).getString("protocol"));
			sr.setSrRemoteGroupId(Sr.getJSONObject(j).getString("remote_group_id"));
			sr.setSrRemoteIpPrefix(Sr.getJSONObject(j).getString("remote_ip_prefix"));
			sr.setSrPortMax(Sr.getJSONObject(j).getString("port_range_max"));
			sr.setSrPortMin(Sr.getJSONObject(j).getString("port_range_min"));
			sr.setSrProjectId(Sr.getJSONObject(j).getString("project_id"));
			sr.setSrTenantId(Sr.getJSONObject(j).getString("tenant_id"));
			sr.setSrSecurityGroupId(Sr.getJSONObject(j).getString("security_group_id"));
			securitygroupSr.add(sr);
		}
		securitygroup.setsecuritygroupSr(securitygroupSr);

		return securitygroup;

	}

	/**
	 *  封装创建安全组所需要传的参数
	 * 用于SecuritygroupImpl类的createSecuritygroup方法
	 * @param securitygroupinput
	 * @return
	 */
	public static JSONObject packageSecuritygroup(Securitygroup securitygroupinput){

		JSONObject params = new JSONObject();
		JSONObject securitygroup = new JSONObject();
		if(securitygroupinput.getsecuritygroupName()!=null)securitygroup.put("name", securitygroupinput.getsecuritygroupName());
		if(securitygroupinput.getSecuritygroupProjectId()!=null)securitygroup.put("project_id", securitygroupinput.getSecuritygroupProjectId());
		if(securitygroupinput.getSecuritygroupTenantId()!=null)securitygroup.put("tenant_id",securitygroupinput.getSecuritygroupTenantId());

		params.put("security_group",securitygroup);

		return params;

	}

	/**
	 * 解析取所有端口返回的结果的JSON
	 * 用于PortImpl类的getRouters方法
	 * @param portsJson
	 * @return
	 */
	public static List<Port> resolvePorts(JSONObject portsJson){

		List<Port> portList = new ArrayList<Port>();
		JSONArray ports = portsJson.getJSONArray("ports");
		System.out.println(ports);
		for(int i = 0 ; i < ports.size(); i++){
			Port port = new Port();
			port.setPortId(ports.getJSONObject(i).getString("id"));
			port.setPortName(ports.getJSONObject(i).getString("name"));
			port.setPortNetworkId(ports.getJSONObject(i).getString("network_id"));
			portList.add(port);
		}

		return portList;

	}

	/**
	 * 封装创建端口所需要传的参数
	 * 用于PortImpl类的createPort方法
	 * @param port
	 * @return
	 */
	public static JSONObject packagePort(Port port) {
		JSONObject params = new JSONObject();
		JSONObject ports = new JSONObject();
		if(port.getPortName()!=null)ports.put("name", port.getPortName());
		if(port.getPortNetworkId()!=null)ports.put("network_id", port.getPortNetworkId());
		if(port.getPortFixedIp()!=null)ports.put("fixed_ips", port.getPortFixedIp());
		params.put("port", ports);
		System.out.println(params);
		return params;
	}

	/**
	 * 解析取创建端口返回的结果的JSON
	 * 用于RouterImpl类的createRouter方法
	 * @param portJson
	 * @return
	 */
	public static Port resolveCreatePort(JSONObject portJson){

		JSONObject ports = portJson.getJSONObject("port");
		Port port = new Port();
		port.setPortName(ports.getString("name"));
		port.setPortId(ports.getString("id"));
		port.setPortNetworkId(ports.getString("network_id"));
		port.setPortFixedIp(ports.getJSONArray("fixed_ips"));
		return port;

	}
	/**
	 * 封装创建、更新防火墙规则所需要传的参数
	 * 用于FwRuleImpl类的createFwRule、updateFwRule方法
	 * @param fwrule
	 * @return
	 */
	public static JSONObject packageFireWallRule(FireWallRule fwrule) {
		JSONObject params = new JSONObject();
		JSONObject fwrules = new JSONObject();
		if(fwrule.getAction()!=null)fwrules.put("action", fwrule.getAction());
		if(fwrule.getDestinationIpAddress()!=null)fwrules.put("destination_ip_address", fwrule.getDestinationIpAddress());
		if(fwrule.getDestinationPort()!=null)fwrules.put("destination_port", fwrule.getDestinationPort());
		if(fwrule.getEnabled()!=null)fwrules.put("enabled", fwrule.getEnabled());
		if(fwrule.getFirewallPolicyId()!=null)fwrules.put("firewall_policy_id", fwrule.getFirewallPolicyId());
		//	if(fwrule.getId()!=null)fwrules.put("action", fwrule.getId());
		if(fwrule.getIpVersion()!=null)fwrules.put("ip_version", fwrule.getIpVersion());
		if(fwrule.getName()!=null)fwrules.put("name", fwrule.getName());
		if(fwrule.getProjectId()!=null)fwrules.put("project_id", fwrule.getProjectId());
		if(fwrule.getProtocol()!=null)fwrules.put("protocol", fwrule.getProtocol());
		if(fwrule.getShared()!=null)fwrules.put("shared", fwrule.getShared());
		if(fwrule.getSourceIpAddress()!=null)fwrules.put("source_ip_address", fwrule.getSourceIpAddress());
		if(fwrule.getSourcePort()!=null)fwrules.put("source_port", fwrule.getSourcePort());
		if(fwrule.getTenantId()!=null)fwrules.put("tenant_id", fwrule.getTenantId());
		params.put("firewall_rule", fwrules);
		return params;

	}
	/**
	 * 解析取所有防火墙规则返回的结果的JSON
	 * 用于FwRuleImpl类的getFireWallRules方法
	 * @param fwruleJson
	 * @return
	 */
	public static List<FireWallRule> resolveFireWallRules(JSONObject fwruleJson) {
		List<FireWallRule> fwruleList = new ArrayList<FireWallRule>();
		JSONArray fwrules = fwruleJson.getJSONArray("firewall_rules");
		for(int i = 0 ; i < fwrules .size(); i++){
			FireWallRule fwrule = new FireWallRule();
			fwrule.setAction(fwrules.getJSONObject(i).getString("action") );
			fwrule.setDestinationIpAddress(fwrules.getJSONObject(i).getString("destination_ip_address"));
			fwrule.setDestinationPort(fwrules.getJSONObject(i).getString("destination_port"));
			fwrule.setEnabled(fwrules.getJSONObject(i).getString("enabled"));
			fwrule.setFirewallPolicyId(fwrules.getJSONObject(i).getString("firewall_policy_id"));
			fwrule.setId(fwrules.getJSONObject(i).getString("id"));
			fwrule.setIpVersion(fwrules.getJSONObject(i).getString("ip_version"));
			fwrule.setName(fwrules.getJSONObject(i).getString("name"));
			fwrule.setProjectId(fwrules.getJSONObject(i).getString("project_id"));
			fwrule.setProtocol(fwrules.getJSONObject(i).getString("protocol"));
			fwrule.setShared(fwrules.getJSONObject(i).getString("shared"));
			fwrule.setSourceIpAddress(fwrules.getJSONObject(i).getString("source_ip_address"));
			fwrule.setSourcePort(fwrules.getJSONObject(i).getString("source_port"));
			fwrule.setTenantId(fwrules.getJSONObject(i).getString("tenant_id"));
			fwruleList.add(fwrule);
		}

		return fwruleList;
	}
	/**
	 * 解析取指定防火墙规则、创建、更新防火墙规则返回的结果的JSON
	 * 用于RFwRuleImpl类的getFwRuleDetail、createFwRule、updateFwRule方法
	 * @param fwruleJson
	 * @return
	 */
	public static FireWallRule resolveFireWallRuleDetail(JSONObject fwruleJson) {
		JSONObject fwrules = fwruleJson.getJSONObject("firewall_rule");
		FireWallRule fwrule = new FireWallRule();
		fwrule.setAction(fwrules.getString("action") );
		fwrule.setDestinationIpAddress(fwrules.getString("destination_ip_address"));
		fwrule.setDestinationPort(fwrules.getString("destination_port"));
		fwrule.setEnabled(fwrules.getString("enabled"));
		fwrule.setFirewallPolicyId(fwrules.getString("firewall_policy_id"));
		fwrule.setId(fwrules.getString("id"));
		fwrule.setIpVersion(fwrules.getString("ip_version"));
		fwrule.setName(fwrules.getString("name"));
		fwrule.setProjectId(fwrules.getString("project_id"));
		fwrule.setProtocol(fwrules.getString("protocol"));
		fwrule.setShared(fwrules.getString("shared"));
		fwrule.setSourceIpAddress(fwrules.getString("source_ip_address"));
		fwrule.setSourcePort(fwrules.getString("source_port"));
		fwrule.setTenantId(fwrules.getString("tenant_id"));

		return fwrule;
	}
	/**
	 * 封装创建、更新防火墙策略所需要传的参数
	 * 用于FwPolicyImpl类的createFwPolicy、updateFwPolicy方法
	 * @param fwpolicy
	 * @return
	 */
	public static JSONObject packageFireWallPolicy(FireWallPolicy fwpolicy) {
		JSONObject params = new JSONObject();
		JSONObject fwpolicies = new JSONObject();
		if(fwpolicy.getAudited()!=null)fwpolicies.put("audited", fwpolicy.getAudited());
		if(fwpolicy.getName()!=null)fwpolicies.put("name", fwpolicy.getName());
		if(fwpolicy.getProjectId()!=null)fwpolicies.put("project_id", fwpolicy.getProjectId());
		if(fwpolicy.getShared()!=null)fwpolicies.put("shared", fwpolicy.getShared());
		if(fwpolicy.getTenantId()!=null)fwpolicies.put("tenant_id", fwpolicy.getTenantId());
		if(fwpolicy.getFirewallRules()!=null)fwpolicies.put("firewall_rules", fwpolicy.getFirewallRules());

		params.put("firewall_policy", fwpolicies);
		return params;

	}
	/**
	 * 解析取所有防火墙策略返回的结果的JSON
	 * 用于FwPolicyImpl类的getFwPolicy方法
	 * @param fwpolicyJson
	 * @return
	 */
	public static List<FireWallPolicy> resolveFireWallPolicies(JSONObject fwpolicyJson) {
		List<FireWallPolicy> fwpolicyList = new ArrayList<FireWallPolicy>();
		JSONArray fwpolicies = fwpolicyJson.getJSONArray("firewall_policies");
		for(int i = 0 ; i < fwpolicies .size(); i++){
			FireWallPolicy fwpolicy = new FireWallPolicy();
			fwpolicy.setAudited(fwpolicies.getJSONObject(i).getString("audited"));
			fwpolicy.setId(fwpolicies.getJSONObject(i).getString("id"));
			fwpolicy.setName(fwpolicies.getJSONObject(i).getString("name"));
			fwpolicy.setProjectId(fwpolicies.getJSONObject(i).getString("project_id"));
			fwpolicy.setShared(fwpolicies.getJSONObject(i).getString("shared"));
			fwpolicy.setTenantId(fwpolicies.getJSONObject(i).getString("tenant_id"));
			ArrayList<String> fwrules = new ArrayList<String>();
			for(int j=0;j<fwpolicies.getJSONObject(i).getJSONArray("firewall_rules").size();j++) {
				fwrules.add(fwpolicies.getJSONObject(i).getJSONArray("firewall_rules").getString(j));
			}
			fwpolicy.setFirewallRules(fwrules);

			fwpolicyList.add(fwpolicy);
		}

		return fwpolicyList;
	}
	/**
	 * 解析取指定防火墙策略、创建、更新防火墙策略、插入、删除防火墙规则返回的结果的JSON
	 * 用于FwPolicyImpl类的getFwPolicyDetail、createFwPolicy、updateFwPolicy、insertFwRule、deleteFwRule方法
	 * @param fwpolicyJson
	 * @return
	 */
	public static FireWallPolicy resolveFireWallPolicyDetail(JSONObject fwpolicyJson) {
		JSONObject fwpolicies = fwpolicyJson.getJSONObject("firewall_policy");
		FireWallPolicy fwpolicy = new FireWallPolicy();
		fwpolicy.setAudited(fwpolicies.getString("audited"));
		fwpolicy.setId(fwpolicies.getString("id"));
		fwpolicy.setName(fwpolicies.getString("name"));
		fwpolicy.setProjectId(fwpolicies.getString("project_id"));
		fwpolicy.setShared(fwpolicies.getString("shared"));
		fwpolicy.setTenantId(fwpolicies.getString("tenant_id"));
		ArrayList<String> fwrules = new ArrayList<String>();
		for(int j=0;j<fwpolicies.getJSONArray("firewall_rules").size();j++) {
			fwrules.add(fwpolicies.getJSONArray("firewall_rules").getString(j));
		}
		fwpolicy.setFirewallRules(fwrules);

		return fwpolicy;
	}
	/**
	 * 封装防火墙策略删除规则所需要传的参数
	 * 用于FwPolicyImpl类的deleteFireWallRule方法
	 * @param fwruleid
	 * @return
	 */
	public static JSONObject packageFireWallRuleRemove(String fwruleid) {

		JSONObject fwrule = new JSONObject();
		if(fwruleid!=null)fwrule.put("firewall_rule_id", fwruleid);
		return fwrule;
	}
	/**
	 * 封装防火墙策略插入规则所需要传的参数
	 * 用于FwPolicyImpl类的insertFireWallRule方法
	 * @param insertrule
	 * @return
	 */
	public static JSONObject packageFireWallRuleInster(FireWallRuleInsert insertrule) {
		JSONObject fwrule = new JSONObject();
		if(insertrule.getFwRuleId()!=null)fwrule.put("firewall_rule_id", insertrule.getFwRuleId());
		if(insertrule.getbeforeFwRuleId()!=null)fwrule.put("insert_before", insertrule.getbeforeFwRuleId());
		if(insertrule.getafterFwRuleId()!=null)fwrule.put("insert_after", insertrule.getafterFwRuleId());
		return fwrule;
	}
	/**
	 * 解析取所有防火墙返回的结果的JSON
	 * 用于FireWallImpl类的getFireWalls方法
	 * @param body
	 * @return
	 */
	public static List<FireWall> resolveFireWalls(JSONObject body) {

		List<FireWall> firewalllist = new ArrayList<FireWall>();
		JSONArray firewalls = body.getJSONArray("firewalls");
		for(int i = 0 ;i < firewalls.size() ; i++){
			FireWall firewall = resolveFirewallsJSON(firewalls.getJSONObject(i));
			firewalllist.add(firewall);
		}
		return firewalllist;

	}

	/**
	 * 解析 查询防火墙返回的结果的JSON
	 * 用于FireWallImpl类的getFireWallDetail方法
	 * @param body
	 * @return
	 */
	public static FireWall resolveFireWallDetail(JSONObject body) {

		JSONObject firewallJson = body.getJSONObject("firewall");
		FireWall firewall = resolveFirewallsJSON(firewallJson);
		return firewall;

	}

	private static FireWall resolveFirewallsJSON(JSONObject firewallJson) {

		FireWall firewall = new FireWall();
		if(firewallJson.getString("admin_state_up") != null)
			firewall.setAdminStateUp(firewallJson.getString("admin_state_up"));
		if(firewallJson.getString("description") != null)
			firewall.setDescription(firewallJson.getString("description"));
		if(firewallJson.getString("firewall_policy_id") != null)
			firewall.setFirewallPolicyId(firewallJson.getString("firewall_policy_id"));
		if(firewallJson.getString("id") != null)
			firewall.setFirewallId(firewallJson.getString("id"));
		if(firewallJson.getString("name") != null)
			firewall.setName(firewallJson.getString("name"));
		if(firewallJson.getString("status") != null)
			firewall.setStatus(firewallJson.getString("status"));
		if(firewallJson.getString("project_id") != null)
			firewall.setProjectId(firewallJson.getString("project_id"));
		if(firewallJson.getString("tenant_id") != null)
			firewall.setTenantId(firewallJson.getString("tenant_id"));
		if(firewallJson.getJSONArray("router_ids") != null){
			List<String> routerIds = new ArrayList<String>();
			int size = firewallJson.getJSONArray("router_ids").size();
			for(int i = 0 ; i < size; i++){
				routerIds.add(firewallJson.getJSONArray("router_ids").getString(i));
			}
			firewall.setRouterIds(routerIds);
		}
		return firewall;

	}

	/**
	 * 封装 创建防火墙 的JSON
	 * 用于FireWallImpl类的createFireWall方法
	 * @param fireWallinpute
	 * @return
	 */
	public static JSONObject packageFireWall(FireWall fireWallinpute) {

		JSONObject params = new JSONObject();
		JSONObject firewall = new JSONObject();
		@SuppressWarnings("unused")
		Object temp = null;
		temp = fireWallinpute.getAdminStateUp() != null ? firewall.put("admin_state_up", fireWallinpute.getAdminStateUp()) : null ;
		temp = fireWallinpute.getFirewallPolicyId() != null ? firewall.put("firewall_policy_id", fireWallinpute.getFirewallPolicyId()) : null ;
		temp = fireWallinpute.getDescription() != null ? firewall.put("description", fireWallinpute.getDescription()) : null ;
		temp = fireWallinpute.getName() != null ? firewall.put("name", fireWallinpute.getName()) : null ;

		if(fireWallinpute.getRouterIds()!=null){
			JSONArray routerIds = new JSONArray();
			int size = fireWallinpute.getRouterIds().size();
			for(int i = 0 ; i < size ; i++){
				routerIds.add(fireWallinpute.getRouterIds().get(i));
			}
			firewall.put("router_ids" , routerIds);
		}
		params.put("firewall", firewall);
		return params;

	}
	public static String getNetworkNumber(String ipAddress) {
		String[] networkNumber = ipAddress.split("\\.");
		if(networkNumber.length >= 3) {
			return networkNumber[0] + "." + networkNumber[1] + "." + networkNumber[2];
		}
		return null;
	}
	public static List<FloatingIp> resolveFloatingIps(JSONObject body) {

		List<FloatingIp> floatingIpList = new ArrayList<FloatingIp>();
		JSONArray floatingIps = body.getJSONArray("floatingips");
		for(int i = 0 ;i < floatingIps.size() ; i++){
			FloatingIp floatip = new FloatingIp();

			floatip.setId(floatingIps.getJSONObject(i).getString("id"));
			floatip.setRouterId(floatingIps.getJSONObject(i).getString("router_id"));
			floatip.setStatus(floatingIps.getJSONObject(i).getString("status"));
			floatip.setDescription(floatingIps.getJSONObject(i).getString("description"));
			floatip.setTenant_id(floatingIps.getJSONObject(i).getString("tenant_id"));
			floatip.setCreated_at(floatingIps.getJSONObject(i).getString("created_at"));
			floatip.setUpdated_at(floatingIps.getJSONObject(i).getString("updated_at"));
			floatip.setFloating_network_id(floatingIps.getJSONObject(i).getString("floating_network_id"));
			floatip.setFixed_ip_address(floatingIps.getJSONObject(i).getString("fixed_ip_address"));
			floatip.setFloating_ip_address(floatingIps.getJSONObject(i).getString("floating_ip_address"));
			floatip.setRevision_number(floatingIps.getJSONObject(i).getString("revision_number"));;
			floatip.setProject_id(floatingIps.getJSONObject(i).getString("project_id"));
			floatip.setPort_id(floatingIps.getJSONObject(i).getString("port_id"));

			floatingIpList.add(floatip);
		}
		return floatingIpList;
	}

	public static FloatingIp resolveFloatingIpDetail(JSONObject body) {

		JSONObject floatIpDetail = body.getJSONObject("floatingip");
		FloatingIp floatingIp = new FloatingIp();

		floatingIp.setId(floatIpDetail.getString("id"));
		floatingIp.setRouterId(floatIpDetail.getString("router_id"));
		floatingIp.setStatus(floatIpDetail.getString("status"));
		floatingIp.setDescription(floatIpDetail.getString("description"));
		floatingIp.setTenant_id(floatIpDetail.getString("tenant_id"));
		floatingIp.setCreated_at(floatIpDetail.getString("created_at"));
		floatingIp.setUpdated_at(floatIpDetail.getString("updated_at"));
		floatingIp.setFloating_network_id(floatIpDetail.getString("floating_network_id"));
		floatingIp.setFixed_ip_address(floatIpDetail.getString("fixed_ip_address"));
		floatingIp.setFloating_ip_address(floatIpDetail.getString("floating_ip_address"));
		floatingIp.setRevision_number(floatIpDetail.getString("revision_number"));;
		floatingIp.setProject_id(floatIpDetail.getString("project_id"));
		floatingIp.setPort_id(floatIpDetail.getString("port_id"));

		return floatingIp;
	}
	public static JSONObject packageFloatingIp(FloatingIp floatingip) {

		JSONObject params = new JSONObject();
		JSONObject floatIp = new JSONObject();

		if(floatingip.getTenant_id() != null ) floatIp.put("tenant_id", floatingip.getTenant_id());
		if(floatingip.getProject_id() != null ) floatIp.put("project_id", floatingip.getProject_id() );
		if(floatingip.getFloating_network_id() != null ) floatIp.put("floating_network_id", floatingip.getFloating_network_id());
		if(floatingip.getFloating_ip_address()!= null ) floatIp.put("floating_ip_address", floatingip.getFloating_ip_address());
		if(floatingip.getFixed_ip_address()!= null ) floatIp.put("fixed_ip_address ", floatingip.getFixed_ip_address());
		if(floatingip.getPort_id() != null ) floatIp.put("port_id", floatingip.getPort_id() );
		if(floatingip.getDescription() != null ) floatIp.put("description", floatingip.getDescription());

		params.put("floatingip",floatIp);

		return params;
	}

	public static JSONObject packageFloatingIpUpdate(FloatingIp floatingip) {

		JSONObject params = new JSONObject();
		JSONObject floatIp = new JSONObject();

		if(floatingip.getFixed_ip_address()!= null ) floatIp.put("fixed_ip_address ", floatingip.getFixed_ip_address());
		if(floatingip.getDescription() != null ) floatIp.put("description", floatingip.getDescription());
		String portId = floatingip.getPort_id() != null? floatingip.getPort_id():"null";
		floatIp.put("port_id", portId);

		params.put("floatingip",floatIp);

		return params;
	}
	public static JSONObject packageGateWay(String router_id, String exNetworkId, String exSubnetId, String ipAddress) {

		JSONObject params = new JSONObject();
		JSONObject router = new JSONObject();
		JSONObject externalGatewayInfo = new JSONObject();
		JSONArray externalFixIp = new JSONArray();
		JSONObject fixIP = new JSONObject();

		externalGatewayInfo.put("network_id", exNetworkId);
		externalGatewayInfo.put("enable_snat", true);

		fixIP.put("ip_address", ipAddress);
		fixIP.put("subnet_id", exSubnetId);
		externalFixIp.add(fixIP);
		externalGatewayInfo.put("external_fixed_ips", externalFixIp);
		router.put("external_gateway_info", externalGatewayInfo);
		params.put("router",router);

		return params;
	}
}

